//
//  TodayViewController.m
//  FoodList
//
//  Created by 王金晶 on 16/4/6.
//  Copyright © 2016年 王金晶. All rights reserved.
//

#import "TodayViewController.h"
#import "TodayMainCell.h"
#import "TableHeaderView.h"
#import "CellDetailController.h"
#import "TodayModel.h"
#import "TodayHeaderModel.h"
#import "RequestManger.h"
#import "FindSecondViewController.h"

@interface TodayViewController ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UILabel *lab1;
@property (nonatomic, strong) UILabel *lab2;
@property (nonatomic, strong) UIView *moveView;

@property (nonatomic, strong) UIPageControl *page;

@property (nonatomic, strong) TableHeaderView *tabViewNew;
@property (nonatomic, strong) TableHeaderView *tabViewHot;

// 承装 model 数组
@property (nonatomic, strong) NSMutableArray *modelArray;
@property (nonatomic, strong) NSMutableArray *headerModelArrNew;
@property (nonatomic, strong) NSMutableArray *headerModelArrHot;

@end

@implementation TodayViewController
// 懒加载
- (NSMutableArray *)modelArray {
    if (!_modelArray) {
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}
- (NSMutableArray *)headerModelArrNew {
    if (!_headerModelArrNew) {
        _headerModelArrNew = [NSMutableArray array];
    }
    return _headerModelArrNew;
}
- (NSMutableArray *)headerModelArrHot {
    if (!_headerModelArrHot) {
        _headerModelArrHot = [NSMutableArray array];
    }
    return _headerModelArrHot;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"今日";
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self creatHeaderView];
    [self requestHeaderViewDataWithIntager:17];
    [self requestHeaderViewDataWithIntager:1002];
    [self requsetCellData];
    
}

- (void)tapActionToDetailVc:(UITapGestureRecognizer *)tap {
        switch (tap.view.tag) {
        case 500: {
            // 跳转详情界面
            FindSecondViewController *cellDetailVc = [[FindSecondViewController alloc] initWithNibName:@"FindSecondViewController" bundle:[NSBundle mainBundle]];
            TableHeaderView *tempView = (TableHeaderView *)tap.view.superview;
            cellDetailVc.myID = tempView.upModel.myId;
            cellDetailVc.name = tempView.upModel.name;
            cellDetailVc.imageURL = tempView.upModel.imageUrl;
            [self.navigationController pushViewController:cellDetailVc animated:YES];
        }
            break;
        case 501: {
            FindSecondViewController *cellDetailVc = [[FindSecondViewController alloc] initWithNibName:@"FindSecondViewController" bundle:[NSBundle mainBundle]];
            TableHeaderView *tempView = (TableHeaderView *)tap.view.superview;
            cellDetailVc.myID = tempView.midModel.myId;
            cellDetailVc.name = tempView.midModel.name;
            cellDetailVc.imageURL = tempView.midModel.imageUrl;
            [self.navigationController pushViewController:cellDetailVc animated:YES];
        }
            break;
        case 502: {
            FindSecondViewController *cellDetailVc = [[FindSecondViewController alloc] initWithNibName:@"FindSecondViewController" bundle:[NSBundle mainBundle]];
            TableHeaderView *tempView = (TableHeaderView *)tap.view.superview;
            cellDetailVc.myID = tempView.downModel.myId;
            cellDetailVc.name = tempView.downModel.name;
            cellDetailVc.imageURL = tempView.downModel.imageUrl;
            [self.navigationController pushViewController:cellDetailVc animated:YES];
        }
            break;
            
        default:
            break;
    }
}
 

#pragma mark - 网络请求 -
- (void)requsetCellData {
    [RequestManger requestWithURL:@"http://www.xdmeishi.com/index.php?m=mobile&c=index&a=getHomeEntity" requestType:@"RequestPOST" parDic:nil finish:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDic = bigDic[@"data"];
        NSArray *moreCookbooksArr = dataDic[@"moreCookbooks"];
        for (NSDictionary *dic in moreCookbooksArr) {
            TodayModel *model = [[TodayModel alloc] init];
            NSString *idStr = dic[@"id"];
            model.myId = idStr;
            [model setValuesForKeysWithDictionary:dic];
            NSDictionary *referrerDic = dic[@"referrer"];
            [model setValuesForKeysWithDictionary:referrerDic];
            [self.modelArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } error:^(NSError *error) {
        NSLog(@"Today Requset fail");
    }];
}


- (void)requestHeaderViewDataWithIntager:(NSInteger)integer {
    [RequestManger requestWithURL:[NSString stringWithFormat:@"http://www.xdmeishi.com/index.php?m=mobile&c=index&a=getRecipeTheme&id=%ld&pageNum=1&pageSize=20", (long)integer] requestType:@"RequestPOST" parDic:nil finish:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *dataArr = bigDic[@"data"];
        if (integer == 17) {
            for (NSDictionary *dic in dataArr) {
                TodayHeaderModel *headerModel = [[TodayHeaderModel alloc] init];
                [headerModel setValuesForKeysWithDictionary:dic];
                headerModel.myId = dic[@"id"];
                [self.headerModelArrNew addObject:headerModel];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                _tabViewNew.modelArr = _headerModelArrNew;
            });
        }else {
            for (NSDictionary *dic in dataArr) {
                TodayHeaderModel *headerModel = [[TodayHeaderModel alloc] init];
                [headerModel setValuesForKeysWithDictionary:dic];
                headerModel.myId = dic[@"id"];
                [self.headerModelArrHot addObject:headerModel];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                _tabViewHot.modelArr = _headerModelArrHot;
            });
        }
    } error:^(NSError *error) {
        NSLog(@"header request fail");
    }];
}
// 创建表头视图
- (void)creatHeaderView {
    UIView *bigView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Width, 400)];
    bigView.backgroundColor = [UIColor colorWithRed:201 / 255.0 green:247 / 255.0 blue:253 / 255.0 alpha:1];
    
    
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake((Width - 80) / 2, 350, 80, 20)];
    _page.enabled = NO;
    _page.currentPage = 0;
    _page.numberOfPages = 2;
    _page.pageIndicatorTintColor = [UIColor grayColor];
    _page.currentPageIndicatorTintColor = [UIColor greenColor];
    [bigView addSubview:_page];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 380, 4, 16)];
    lineView.backgroundColor = [UIColor redColor];
    lineView.layer.cornerRadius = 2;
    [bigView addSubview:lineView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 380, 60, 16)];
    titleLabel.text = @"每日推荐";
    titleLabel.font = [UIFont systemFontOfSize:13];
    [bigView addSubview:titleLabel];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, Width, 340)];
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(Width * 2, 340);
    _scrollView.contentOffset = CGPointMake(0, 0);
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    
    // 创建 headerView 上两个 View
    self.tabViewNew = [[NSBundle mainBundle] loadNibNamed:@"TableHeaderView" owner:nil options:nil].firstObject;
    _tabViewNew.frame = CGRectMake(0, 0, Width, 340);
    [_scrollView addSubview:_tabViewNew];
    
    UITapGestureRecognizer *upTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionToDetailVc:)];
    [self.tabViewNew.upImage addGestureRecognizer:upTap];
    self.tabViewNew.upImage.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *midTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionToDetailVc:)];
    [self.tabViewNew.midImage addGestureRecognizer:midTap];
    self.tabViewNew.midImage.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *downTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionToDetailVc:)];
    [self.tabViewNew.downImage addGestureRecognizer:downTap];
    self.tabViewNew.downImage.userInteractionEnabled = YES;
    
    self.tabViewHot = [[NSBundle mainBundle] loadNibNamed:@"TableHeaderView" owner:nil options:nil].firstObject;
    _tabViewHot.frame = CGRectMake(Width, 0, Width, 340);
    _tabViewHot.modelArr = _headerModelArrHot;
    [_scrollView addSubview:_tabViewHot];
    
    UITapGestureRecognizer *upTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionToDetailVc:)];
    [self.tabViewHot.upImage addGestureRecognizer:upTap1];
    self.tabViewHot.upImage.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *midTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionToDetailVc:)];
    [self.tabViewHot.midImage addGestureRecognizer:midTap1];
    self.tabViewHot.midImage.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *downTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionToDetailVc:)];
    [self.tabViewHot.downImage addGestureRecognizer:downTap1];
    self.tabViewHot.downImage.userInteractionEnabled = YES;
    
    [bigView addSubview:_scrollView];
    self.tableView.tableHeaderView = bigView;
}
#pragma mark - scrollView 代理方法 -
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == 0) {
        _page.currentPage = 0;
    }else {
        _page.currentPage = 1;
    }
}
#pragma mark - tableView 代理方法 -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TodayMainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"todayCell"];
    if (cell == nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"TodayMainCell" owner:nil options:nil].firstObject;
    }
    cell.model = _modelArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    FindSecondViewController *cellDetailVc = [[FindSecondViewController alloc] initWithNibName:@"FindSecondViewController" bundle:[NSBundle mainBundle]];
    TodayModel *model = _modelArray[indexPath.row];
    cellDetailVc.myID = model.myId;
    cellDetailVc.name = model.name;
    cellDetailVc.imageURL = model.imageUrl;
    [self.navigationController pushViewController:cellDetailVc animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
